import { requestUpload } from '@/utils/request'

export type UploadResult = {
  fullName: string
  isVideo: boolean
  name: string
  videoCoverImg: string | null
  videoDuration: number | null
}
/** 文件上传 */
export function uploadFile(
  file: File,
  extraData?: Recordable,
): Promise<UploadResult> {
  const formData = new FormData()
  formData.append('file', file)

  // 如果有额外的数据，将它们追加到 FormData 中
  if (extraData) {
    Object.keys(extraData).forEach((key) => {
      formData.append(key, extraData[key])
    })
  }

  return requestUpload.post('/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  })
}
